package com.joyintech.modules.pmp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.joyintech.base.PageInfo;
import com.joyintech.base.QueryHelpMybatisPlus;
import com.joyintech.base.impl.CommonServiceImpl;
import com.joyintech.modules.pmp.domain.DsApp;
import com.joyintech.modules.pmp.service.DsAppService;
import com.joyintech.modules.pmp.service.dto.DsAppDto;
import com.joyintech.modules.pmp.service.dto.DsAppQueryParam;
import com.joyintech.modules.pmp.service.mapper.DsAppMapper;
import com.joyintech.utils.ConvertUtil;
import com.joyintech.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author daiweiwei
* @date 2021-09-27
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DsAppService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DsAppServiceImpl extends CommonServiceImpl<DsAppMapper, DsApp> implements DsAppService {

    // private final RedisUtils redisUtils;
    private final DsAppMapper dsAppMapper;

    @Override
    public PageInfo<DsAppDto> queryAll(DsAppQueryParam query, Pageable pageable) {
        IPage<DsApp> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DsApp> page = dsAppMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DsAppDto.class);
    }

    @Override
    public List<DsAppDto> queryAll(DsAppQueryParam query){
        return ConvertUtil.convertList(dsAppMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DsAppDto.class);
    }

    @Override
    public DsApp getById(String id) {
        return dsAppMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DsAppDto findById(String id) {
        return ConvertUtil.convert(getById(id), DsAppDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DsAppDto resources) {
        DsApp entity = ConvertUtil.convert(resources, DsApp.class);
        return dsAppMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DsAppDto resources){
        DsApp entity = ConvertUtil.convert(resources, DsApp.class);
        int ret = dsAppMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return dsAppMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DsAppDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DsAppDto dsApp : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("项目名称", dsApp.getName());
              map.put("项目简介", dsApp.getAppDesc());
              map.put("甲方项目经理", dsApp.getPmpA());
              map.put("乙方项目经理", dsApp.getPmpB());
              map.put("gitlab路径", dsApp.getGitlabUrl());
              map.put("预计上线日期", dsApp.getOnlineTime());
              map.put("关联项目需求编号", dsApp.getPrdId());
              map.put("创建者", dsApp.getCreateBy());
              map.put("更新者", dsApp.getUpdateBy());
              map.put("创建日期", dsApp.getCreateTime());
              map.put("更新时间", dsApp.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
